/*********************************** (C) COPYRIGHT ******************************
 * address: https://gitee.com/liuniansihuo/multitasks-os
 *
 * Change Logs:
 * Date			Author      	Notes
 * 2019-06-29	LiuHuo			first version
 *
*******************************************************************************/
#ifndef __os_H_
#define __os_H_

#define BIG_ENDIAN 		0
#define LITTLE_ENDIAN	1
#define ENDIAN_TYPE		LITTLE_ENDIAN

#if ENDIAN_TYPE == LITTLE_ENDIAN
typedef union U_U8_VAL_S
{
	unsigned char val;
	unsigned char buff[1];
	char ch;
	struct{	unsigned char LB:8;	} byte;
	struct
	{
		unsigned char bit0:1; 	unsigned char bit1:1;	unsigned char bit2:1;	unsigned char bit3:1;
		unsigned char bit4:1;	unsigned char bit5:1;	unsigned char bit6:1;	unsigned char bit7:1;
	} bit;

}U_8_T;
typedef union U_U16_VAL_S
{
	unsigned short int usi;
	unsigned char buff[2];
	short int si;

	struct{unsigned short LB:8;	unsigned short HB:8;} Byte;
	struct
	{
		unsigned short bit0:1;	unsigned short bit1:1;	unsigned short bit2:1;	unsigned short bit3:1;
		unsigned short bit4:1;	unsigned short bit5:1;	unsigned short bit6:1;	unsigned short bit7:1;
		unsigned short bit8:1;	unsigned short bit9:1;	unsigned short bit10:1;	unsigned short bit11:1;
		unsigned short bit12:1;	unsigned short bit13:1;	unsigned short bit14:1;	unsigned short bit15:1;
	} bit;

}U_16_T;
typedef union U_U32_VAL_S
{
	unsigned  int ui;
	unsigned char buff[4];
	float fl;
	int ii;
	struct{unsigned long LLB:8;	unsigned long MLB:8;	unsigned long MHB:8;	unsigned long HHB:8;} byte;
	struct
	{
		unsigned long bit0:1;	unsigned long bit1:1;	unsigned long bit2:1;	unsigned long bit3:1;
		unsigned long bit4:1;	unsigned long bit5:1;	unsigned long bit6:1;	unsigned long bit7:1;
		unsigned long bit8:1;	unsigned long bit9:1;	unsigned long bit10:1;	unsigned long bit11:1;
		unsigned long bit12:1;	unsigned long bit13:1;	unsigned long bit14:1;	unsigned long bit15:1;
		unsigned long bit16:1;	unsigned long bit17:1;	unsigned long bit18:1;	unsigned long bit19:1;
		unsigned long bit20:1;	unsigned long bit21:1;	unsigned long bit22:1;	unsigned long bit23:1;
		unsigned long bit24:1;	unsigned long bit25:1;	unsigned long bit26:1;	unsigned long bit27:1;
		unsigned long bit28:1;	unsigned long bit29:1;	unsigned long bit30:1;	unsigned long bit32:1;
	} bit;
}U_32_T;
#else

#endif

#define BIT_SET(D,P)	do{(D) |= (1<<(P));}while(0)
#define BIT_CLR(D,P)	do{(D) &= ~(1<<(P));}while(0)
#define BIT_GET(D,P)	(!(!( (D) & (1<<(P)) )))

/**********************************
* 0x12345678: HH:0x12 MH:0x34 ML:0x56 LL:0x78
* 0xAABB	: HH:0xAA LL:0xBB
***********************************/

//强制转换为u8*指针
#define ptopu8(p)		((u8*)(p))
#define pu16topu8(p)	ptopu8(p)
#define pu32topu8(p)	ptopu8(p)
#define pi32topu8(p)	ptopu8(p)
#define pf32topu8(p)	ptopu8(p)




//字，半字分解为字节
#define u32HHtou8(D)	(u8)( ((D)>>24)&0xFF )
#define u32MHtou8(D)	(u8)( ((D)>>16)&0xFF )
#define u32MLtou8(D)	(u8)( ((D)>>8)&0xFF )
#define u32LLtou8(D)	(u8)( (D)&0xFF )
#define u16HHtou8(D)	(u8)( ((D)>>8)&0xFF )
#define u16LLtou8(D)	(u8)( (D)&0xFF )


//半字，字，浮点，整型转换为字节，通过地址
#define pu16HHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pu16topu8(p)[1]) : (pu16topu8(p)[0])
#define pu16LLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pu16topu8(p)[0]) : (pu16topu8(p)[1])

#define pu32HHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pu32topu8(p)[3]) : (pu32topu8(p)[0])
#define pu32MHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pu32topu8(p)[2]) : (pu32topu8(p)[1])
#define pu32MLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pu32topu8(p)[1]) : (pu32topu8(p)[2])
#define pu32LLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pu32topu8(p)[0]) : (pu32topu8(p)[3])

#define pf32HHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pf32topu8(p)[3]) : (pf32topu8(p)[0])
#define pf32MHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pf32topu8(p)[2]) : (pf32topu8(p)[1])
#define pf32MLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pf32topu8(p)[1]) : (pf32topu8(p)[2])
#define pf32LLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pf32topu8(p)[0]) : (pf32topu8(p)[3])

#define pi32HHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pi32topu8(p)[3]) : (pi32topu8(p)[0])
#define pi32MHtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pi32topu8(p)[2]) : (pi32topu8(p)[1])
#define pi32MLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pi32topu8(p)[1]) : (pi32topu8(p)[2])
#define pi32LLtou8(p)	ENDIAN_TYPE==LITTLE_ENDIAN ? (pi32topu8(p)[0]) : (pi32topu8(p)[3])



//字节组合成字，半字
#define u8tou16(DHH,DLL) 	(u16)(((DHH)<<8) | (DLL))
#define u8tou32(DHH,DMH,DML,DLL) 	(u32)(((DHH)<<24) | ((DMH)<<16) | ((DML)<<8) | (DLL))





//字，半字分解到字节缓存，分大小端
#define u16topu8(D,p)	do{ (p)[0]= ENDIAN_TYPE==LITTLE_ENDIAN ? u16LLtou8(D) : u16HHtou8(D);\
							(p)[1]= ENDIAN_TYPE==LITTLE_ENDIAN ? u16HHtou8(D) : u16LLtou8(D);}while(0)

#define u32topu8(D,p)	do{ (p)[0]= ENDIAN_TYPE==LITTLE_ENDIAN ? u32LLtou8(D) : u32HHtou8(D);\
							(p)[1]= ENDIAN_TYPE==LITTLE_ENDIAN ? u32MLtou8(D) : u32MHtou8(D);\
							(p)[2]= ENDIAN_TYPE==LITTLE_ENDIAN ? u32MHtou8(D) : u32MLtou8(D);\
							(p)[3]= ENDIAN_TYPE==LITTLE_ENDIAN ? u32HHtou8(D) : u32LLtou8(D);}while(0)




//短整形高低字节交换
#define Swap16(D) ((( (u16)(D)&0x00FF )<<8) | (( (u16)(D)&0xFF00 )>>8))
//长整形高低字节交换
#define Swap32(D) ((( (u32)(D)&0x000000FF )<<24) | (( (u32)(D)&0x0000FF00 )<<8) | (( (u32)(D)&0x00FF0000 )>>8) | (( (u32)(D)&0xFF000000 )>>24))

//网络字序与本机字序交换
#define ntohl(D)	(ENDIAN_TYPE == LITTLE_ENDIAN ? Swap32(D) : D)
#define ntohs(D)	(ENDIAN_TYPE == LITTLE_ENDIAN ? Swap16(D) : D)
#define htonl(D)	(ENDIAN_TYPE == LITTLE_ENDIAN ? Swap32(D) : D)
#define htons(D)	(ENDIAN_TYPE == LITTLE_ENDIAN ? Swap16(D) : D)


//链接宏定义
#define __CONNECT2(__0, __1)            __0##__1
//! 安全宏定义
#define CONNECT2(__0, __1)             __CONNECT2(__0, __1)
//获取参数序号
#define VA_NUM_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, __N,...) __N
#define VA_NUM_ARGS(...)	VA_NUM_ARGS_IMPL(__VA_ARGS__,9,8,7,6,5,4,3,2,1)

//获取宏变参指定参数
#define _GET_ARG_1(_1, ...)             _1
#define _GET_ARG_2(_1, _2, ...)         _2
#define _GET_ARG_3(_1, _2, _3,...)      _3
#define GET_VA_ARG_1(...)              _GET_ARG_1(__VA_ARGS__)



typedef void (*function_task)(void);


inline u8 endian_type(void)
{
	 union
	{
		   u32 i;
		   u8 c;
	}u={.i=0x1};
	return u.c==0x1 ? LITTLE_ENDIAN : BIG_ENDIAN ;
}



#endif

